<!--
 * @Description: 文件描述
 * @Author: 程前
 * @Date: 2024-12-06 15:14:38
 * @LastEditors: 程前
 * @LastEditTime: 2024-12-24 16:55:31
-->

<template>
    <div class="nav-bar">
        <div class="nav-item-wrapper">
            <div class="nav-item">{{ navItems[0].text }}</div>
            <div class="scale-bar-wrapper">
                <div class="scale-bar">
                    <div class="scale-segment white"></div>
                </div>
            </div>
        </div>
        <div class="right-info">
            <div v-for="(item, index) in navItems.slice(1)" :key="index" class="nav-item">
                <span class="label">{{ item.label }}:</span>
                <span class="value">{{ item.value }}</span>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount } from "vue";
import { useCesiumGlobalStore } from "@/store/modules/cesiun-global"
import CesiumBarInfo from "@/cesium/navbar"
import { eventBus } from '@/utils';

const cesiumGlobalStore = useCesiumGlobalStore()
const navItems = ref<any[]>([
    { text: '' },
    { label: '经度', value: '' },
    { label: '纬度', value: '' },
    { label: '高度', value: '' },
    { label: '视点高度', value: '' }
])

let cesiumBarInfo: any = null
onMounted(() => {
    const timer = setInterval(() => {
        const viewer = cesiumGlobalStore.viewerInstance
        if (!viewer) return false
        cesiumBarInfo = new CesiumBarInfo(viewer)

        eventBus.on('cesiumBarInfo', (e: any) => {
            const scaleValue = e.metersPerCm / 1000;
            navItems.value[0].text = scaleValue >= 1 ? `${scaleValue.toFixed(2)}KM` : `${(scaleValue * 1000).toFixed(0)}M`

            const longitude = Math.abs(e.longitude);
            const lonDir = Number(e.longitude) >= 0 ? 'E' : 'W';
            navItems.value[1].value = `${lonDir}${longitude}`;

            const latitude = Math.abs(e.latitude);
            const latDir = Number(e.latitude) >= 0 ? 'N' : 'S';
            navItems.value[2].value = `${latDir}${latitude}`;

            navItems.value[3].value = `${e.height}M`
            navItems.value[4].value = `${(e.cameraHeight / 1000).toFixed(1)}KM`
        })

        clearInterval(timer)
    }, 500)
})

onBeforeUnmount(() => {
    if (cesiumBarInfo) {
        cesiumBarInfo.destroy()
    }
    eventBus.off('cesiumBarInfo')
})
</script>


<style scoped lang="less">
.nav-bar {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 50px;
    background-color: rgba(51, 51, 51, 0.8);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.nav-item {
    font-size: 18px;
    font-family: Arial, sans-serif;
    white-space: nowrap;

    .label {
        color: rgba(255, 255, 255, 0.8);
        margin-right: 4px;
    }

    .value {
        color: #fff;
    }
}

.nav-item-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    padding: 2px 4px;
    border-radius: 2px;
    margin-left: 25px
}

.scale-bar-wrapper {
    border: 1px solid rgba(255, 255, 255, 0.8);
    padding: 1px;
    background: rgba(0, 0, 0, 0.2);
}

.scale-bar {
    width: 100px;
    height: 3px;
    display: flex;
}

.scale-segment {
    width: 100%;
    height: 100%;

    &.white {
        background-color: #fff;
    }
}

.right-info {
    display: flex;
    gap: 40px;
    align-items: center;
    margin-right: 55px;
}
</style>